package com.atguigu.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.base.BaseDao;
import com.atguigu.base.BaseServiceImpl;
import com.atguigu.dao.AdminRoleDao;
import com.atguigu.dao.RoleDao;
import com.atguigu.entity.AdminRole;
import com.atguigu.entity.Role;
import com.atguigu.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl extends BaseServiceImpl<Role> implements RoleService {

//    @Autowired  //Spring框架提供的依赖注入注解 先byType再byName
    @Resource   //jdk提供的依赖注入     先byName  再 byType
    RoleDao roleDao;

    @Autowired
    AdminRoleDao adminRoleDao;

    @Override
    public BaseDao<Role> getEntityDao() {
        return roleDao;
    }

    @Override
    public List<Role> findAll() {
        return roleDao.findAll();
    }

    @Override
    public Map getSelectByAdminId(Long id) {
        Map map = new HashMap<>();
        ArrayList noAssignRoleList = new ArrayList();
        ArrayList assignRoleList = new ArrayList();
        //1.查询所以的角色
        List<Role> allList = roleDao.findAll();
        //2.查询该用户所拥有的角色id集合
        List<Long> roleIdList =  adminRoleDao.finRoleIdListByAdminId(id);
        //3.将所有角色划分到两个集合中返回
        for (Role role : allList) {
            Long roleId = role.getId();
            if (roleIdList.contains(roleId)) { //迭代角色id 如果易用的角色集合中存在，说明这个角色已分配
                assignRoleList.add(role);
            }else {
                noAssignRoleList.add(role);
            }
        }

        map.put("noAssignRoleList", noAssignRoleList);
        map.put("assignRoleList", assignRoleList);
        return map;
    }

    @Override
    public void assignRole(Long adminId, Long[] roleIds) {
        //1.先删除已有的角色
        adminRoleDao.deleteAdminRoleRelationship(adminId);
        //2.重新分配新的角色
        List list = new ArrayList();
        if (roleIds != null && roleIds.length > 0) {
            for (Long roleId : roleIds) {
                if (StringUtils.isEmpty(roleId)) {
                    continue;
                }
                AdminRole adminRole = new AdminRole();
                adminRole.setAdminId(adminId);
                adminRole.setRoleId(roleId);
                list.add(adminRole);
            }
        }
        adminRoleDao.insertBatch(list);

    }


}
